const Router = require("koa-router");
const router = new Router();

// 使用相同的充电站数据结构
const chargingStations = [
  // 保持您提供的所有充电站数据不变
  {
    name: "北京西单充电站",
    id: "VXZ10001",
    city: "北京",
    fast: 95,
    slow: 40,
    status: 3,
    now: 10,
    fault: 1,
    person: "张伟",
    tel: 17876554801,
  },
  {
    name: "上海陆家嘴充电站",
    id: "VXZ10002",
    city: "上海",
    fast: 90,
    slow: 35,
    status: 2,
    now: 20,
    fault: 2,
    person: "李娜",
    tel: 17876554802,
  },
  {
    name: "广州花城广场充电站",
    id: "VXZ10003",
    city: "广州",
    fast: 85,
    slow: 38,
    status: 2,
    now: 5,
    fault: 0,
    person: "王强",
    tel: 17876554803,
  },
  {
    name: "深圳大梅沙充电站",
    id: "VXZ10004",
    city: "深圳",
    fast: 80,
    slow: 30,
    status: 3,
    now: 15,
    fault: 1,
    person: "赵敏",
    tel: 17876554804,
  },
  {
    name: "成都天府广场充电站",
    id: "VXZ10005",
    city: "成都",
    fast: 88,
    slow: 37,
    status: 5,
    now: 12,
    fault: 2,
    person: "李晓华",
    tel: 17876554805,
  },
  {
    name: "西安钟楼充电站",
    id: "VXZ10006",
    city: "西安",
    fast: 82,
    slow: 33,
    status: 4,
    now: 8,
    fault: 0,
    person: "刘伟",
    tel: 17876554806,
  },
  {
    name: "杭州西湖充电站",
    id: "VXZ10007",
    city: "杭州",
    fast: 75,
    slow: 29,
    status: 3,
    now: 18,
    fault: 3,
    person: "陈芳",
    tel: 17876554807,
  },
  {
    name: "南京夫子庙充电站",
    id: "VXZ10008",
    city: "南京",
    fast: 89,
    slow: 40,
    status: 2,
    now: 22,
    fault: 1,
    person: "黄伟",
    tel: 17876554808,
  },
  {
    name: "天津意大利风情区充电站",
    id: "VXZ10009",
    city: "天津",
    fast: 87,
    slow: 36,
    status: 2,
    now: 6,
    fault: 0,
    person: "吴敏",
    tel: 17876554809,
  },
  {
    name: "青岛栈桥充电站",
    id: "VXZ10010",
    city: "青岛",
    fast: 91,
    slow: 32,
    status: 3,
    now: 25,
    fault: 1,
    person: "杨杰",
    tel: 17876554810,
  },
  {
    name: "武汉黄鹤楼充电站",
    id: "VXZ10011",
    city: "武汉",
    fast: 74,
    slow: 28,
    status: 2,
    now: 11,
    fault: 2,
    person: "周丽",
    tel: 17876554811,
  },
  {
    name: "福州三坊七巷充电站",
    id: "VXZ10012",
    city: "福州",
    fast: 76,
    slow: 31,
    status: 4,
    now: 19,
    fault: 1,
    person: "林涛",
    tel: 17876554812,
  },
  {
    name: "合肥包公园充电站",
    id: "VXZ10013",
    city: "合肥",
    fast: 70,
    slow: 30,
    status: 2,
    now: 3,
    fault: 0,
    person: "高峰",
    tel: 17876554813,
  },
  {
    name: "重庆解放碑充电站",
    id: "VXZ10014",
    city: "重庆",
    fast: 83,
    slow: 34,
    status: 2,
    now: 14,
    fault: 2,
    person: "邓超",
    tel: 17876554814,
  },
  {
    name: "桂林漓江充电站",
    id: "VXZ10015",
    city: "桂林",
    fast: 77,
    slow: 29,
    status: 2,
    now: 9,
    fault: 1,
    person: "李曼",
    tel: 17876554815,
  },
  {
    name: "苏州园区充电站",
    id: "VXZ10016",
    city: "苏州",
    fast: 80,
    slow: 35,
    status: 2,
    now: 7,
    fault: 0,
    person: "王琦",
    tel: 17876554816,
  },
  {
    name: "昆明滇池充电站",
    id: "VXZ10017",
    city: "昆明",
    fast: 79,
    slow: 33,
    status: 3,
    now: 21,
    fault: 1,
    person: "陈超",
    tel: 17876554817,
  },
  {
    name: "南宁青秀山充电站",
    id: "VXZ10018",
    city: "南宁",
    fast: 81,
    slow: 36,
    status: 2,
    now: 13,
    fault: 0,
    person: "蔡徐坤",
    tel: 17876554818,
  },
  {
    name: "长沙橘子洲头充电站",
    id: "VXZ10019",
    city: "长沙",
    fast: 78,
    slow: 34,
    status: 2,
    now: 4,
    fault: 0,
    person: "李安",
    tel: 17876554819,
  },
  {
    name: "哈尔滨中央大街充电站",
    id: "VXZ10020",
    city: "哈尔滨",
    fast: 75,
    slow: 32,
    status: 2,
    now: 23,
    fault: 1,
    person: "林雨",
    tel: 17876554820,
  },
  {
    name: "石家庄正定古城充电站",
    id: "VXZ10021",
    city: "石家庄",
    fast: 72,
    slow: 31,
    status: 4,
    now: 16,
    fault: 2,
    person: "郑明",
    tel: 17876554821,
  },
  {
    name: "兰州黄河桥充电站",
    id: "VXZ10022",
    city: "兰州",
    fast: 88,
    slow: 38,
    status: 5,
    now: 5,
    fault: 0,
    person: "李伟",
    tel: 17876554822,
  },
  {
    name: "济南大明湖充电站",
    id: "VXZ10023",
    city: "济南",
    fast: 93,
    slow: 39,
    status: 3,
    now: 17,
    fault: 1,
    person: "刘洋",
    tel: 17876554823,
  },
  {
    name: "沈阳故宫充电站",
    id: "VXZ10024",
    city: "沈阳",
    fast: 76,
    slow: 32,
    status: 4,
    now: 19,
    fault: 1,
    person: "张博",
    tel: 17876554824,
  },
  {
    name: "福州西湖充电站",
    id: "VXZ10025",
    city: "福州",
    fast: 79,
    slow: 34,
    status: 2,
    now: 6,
    fault: 0,
    person: "周扬",
    tel: 17876554825,
  },
  {
    name: "无锡灵山大佛充电站",
    id: "VXZ10026",
    city: "无锡",
    fast: 85,
    slow: 38,
    status: 3,
    now: 12,
    fault: 1,
    person: "江海",
    tel: 17876554826,
  },
  {
    name: "郑州二七广场充电站",
    id: "VXZ10027",
    city: "郑州",
    fast: 84,
    slow: 36,
    status: 3,
    now: 20,
    fault: 2,
    person: "胡丽",
    tel: 17876554827,
  },
  {
    name: "大连星海广场充电站",
    id: "VXZ10028",
    city: "大连",
    fast: 82,
    slow: 35,
    status: 2,
    now: 7,
    fault: 0,
    person: "李涛",
    tel: 17876554828,
  },
  {
    name: "宁波天一广场充电站",
    id: "VXZ10029",
    city: "宁波",
    fast: 90,
    slow: 40,
    status: 4,
    now: 15,
    fault: 1,
    person: "程伟",
    tel: 17876554829,
  },
  {
    name: "贵阳甲秀楼充电站",
    id: "VXZ10030",
    city: "贵阳",
    fast: 81,
    slow: 33,
    status: 2,
    now: 14,
    fault: 2,
    person: "马云",
    tel: 17876554830,
  },
  {
    name: "珠海长隆海洋王国充电站",
    id: "VXZ10031",
    city: "珠海",
    fast: 78,
    slow: 36,
    status: 2,
    now: 11,
    fault: 3,
    person: "王超",
    tel: 17876554831,
  },
  {
    name: "天津滨海新区充电站",
    id: "VXZ10032",
    city: "天津",
    fast: 92,
    slow: 37,
    status: 2,
    now: 8,
    fault: 0,
    person: "黄华",
    tel: 17876554832,
  },
];

// ==================== 优化后的新增/编辑充电站接口 ==================== 
router.post("/charging-station/edit", async (ctx) => {
  try {
    const stationData = ctx.request.body;
    console.log("新增/编辑充电站接口收到数据：", stationData);

    // 数据验证 - 增强字段校验
    const requiredFields = ['name', 'city', 'fast', 'slow', 'status'];
    const missingFields = requiredFields.filter(field => !stationData[field] && stationData[field] !== 0);
    
    if (missingFields.length > 0) {
      ctx.body = {
        code: 400,
        success: false,
        message: `缺少必填字段: ${missingFields.join(', ')}`
      };
      return;
    }

    // ======== 问题1解决方案：确保数据引用更新 ======== //
    // 编辑逻辑 - 根据ID查找现有充电站
    if (stationData.id) {
      const index = chargingStations.findIndex(s => s.id === stationData.id);
      
      if (index === -1) {
        // ======== 问题2解决方案：处理ID不存在的情况 ======== //
        // 如果ID不存在，转为新增逻辑
        console.warn(`充电站ID ${stationData.id} 不存在，转为新增操作`);
        
        // 生成符合规范的ID（确保ID格式正确）
        const ids = chargingStations.map(s => parseInt(s.id.replace("VXZ", "")));
        const maxId = ids.length > 0 ? Math.max(...ids) : 10000;
        const newId = "VXZ" + (maxId + 1).toString().padStart(5, "0");

        // 构建完整充电站对象
        const newStation = {
          id: newId,
          now: 0,
          fault: 0,
          ...stationData
        };

        chargingStations.push(newStation);
        console.log(`新增充电站成功，ID: ${newId}`);

        ctx.body = {
          code: 200,
          success: true,
          message: "新增成功（原ID不存在）",
          data: newStation
        };
        return;
      }

      // ======== 核心修改：确保数据引用更新 ======== //
      // 创建新对象而不是修改原对象
      const updatedStation = {
        ...chargingStations[index], // 原始数据
        ...stationData,             // 新提交数据
        
        // 保护关键字段不被覆盖
        id: chargingStations[index].id,
        now: chargingStations[index].now,
        fault: chargingStations[index].fault
      };

      // 替换数组中的引用（确保前端能检测到变化）
      chargingStations.splice(index, 1, updatedStation);

      ctx.body = {
        code: 200,
        success: true,
        message: "更新成功",
        data: updatedStation
      };
    } 
    // 新增逻辑
    else {
      // 生成符合规范的ID（VXZ+5位数字）
      const ids = chargingStations.map(s => parseInt(s.id.replace("VXZ", "")));
      const maxId = ids.length > 0 ? Math.max(...ids) : 10000;
      const newId = "VXZ" + (maxId + 1).toString().padStart(5, "0");

      // 构建完整充电站对象
      const newStation = {
        id: newId,
        now: 0,
        fault: 0,
        ...stationData
      };

      chargingStations.push(newStation);
      console.log(`新增充电站成功，ID: ${newId}`);

      ctx.body = {
        code: 200,
        success: true,
        message: "新增成功",
        data: newStation
      };
    }
  } catch (error) {
    console.error("新增/编辑充电站接口出错：", error);
    ctx.body = {
      code: 500,
      success: false,
      message: `服务器内部错误: ${error.message}`
    };
  }
});

// 导出路由
module.exports = router;